from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api import wish,user
from database.database import init_db, close_db

app = FastAPI()

# 创建FastAPI应用并配置文档
app = FastAPI(
    title="心愿小程序API",
    description="心愿小程序后端API文档",
    version="1.0.0",
    docs_url="/docs",  # 显式指定Swagger UI的URL
    redoc_url="/redoc",  # 显式指定ReDoc的URL
    openapi_url="/openapi.json"  # 显式指定OpenAPI JSON的URL
)
# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 包含路由
app.include_router(user.router, prefix="/api", tags=["users"])
app.include_router(wish.router, prefix="/api", tags=["wishes"])

@app.on_event("startup")
async def startup():
    await init_db()

@app.on_event("shutdown")
async def shutdown():
    await close_db()



